window.onload = function () {
    fetch('./data.json').then(response => response.json())
        .then(data => {
            let result = getcnTitle(data, 'c');
            console.log(result);
        })
        .catch(e => console.log(e))

    function getcnTitle(data, entitle) {
        let result = "";
        if (data.en == entitle) {
            result = data.cn;
        } else if (data.extra != undefined) {
            data.extra.forEach((v, index) => {
                if (result == "")
                    result = getcnTitle(v, entitle);
            });
        }
        return result;
    }

    let chapterArr = ["# a", "## b", "## c", "### d", "# e"];
    getChapter(chapterArr);
    function getChapter(data) {
        let result = [], hn = [];
        result = data.map(function (v, index) {
            let len = v.split(" ")[0].length;
            if (len == 1) {
                hn = index == 0 ? [1] : [hn[0] + 1];
            } else {
                hn.length = len;
                hn[len - 1] = hn[len - 1] == undefined ? 1 : hn[len - 1] + 1;
            }
            return {
                hn: hn.join("."),
                title: v.split(" ")[1]
            }
        })
        return result;
    }
}